using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.Configuration;
using QuickLaunch.Properties;

namespace QuickLaunch
{
    static class Program
    {
        #region Settings and utilities

        public static QuickSetting qlDirSetting = null;

        public static QuickSetting qlShortcutSetting = null;

        public static QuickSetting qlOtherSettings = null;

        public static FileNameSearch fileNameSearch = null;

        public static FormManagement formManager = null;

        static void Initialize()
        {
            // initialize the settings
            qlDirSetting = new QuickSetting(Application.StartupPath, "qldirs.conf");
            qlShortcutSetting = new QuickSetting(Application.StartupPath, "shortcuts.conf");
            qlOtherSettings = new QuickSetting(Application.StartupPath, "setting.conf");

            // initialize the searcher
            // get the exclude extensions
            string excludes = qlOtherSettings.GetValue(Resources.ExcludeExt), ext;
            List<string> excludeExts = null;
            if (!string.IsNullOrEmpty(excludes))
            {
                excludes = excludes.Trim().ToLower();
                excludeExts = new List<string>();
                foreach (string extTmp in excludes.Split(';'))
                {
                    ext = extTmp.Trim();
                    if (!string.IsNullOrEmpty(ext))
                        excludeExts.Add(ext);
                }
            }
            fileNameSearch = new FileNameSearch(qlDirSetting.GetSettingItems(), excludeExts, 2);
            string historyFile = Application.StartupPath + "\\search.his";
            fileNameSearch.ApplySearchHistory(historyFile);

            // register some events
            qlDirSetting.SettingChanged += new SettingChangedEventHandler(qlDirSetting_SettingChanged);
            qlShortcutSetting.SettingChanged += new SettingChangedEventHandler(qlShortcutSetting_SettingChanged);
            qlOtherSettings.SettingChanged += new SettingChangedEventHandler(qlOtherSettings_SettingChanged);
            
            // initialize the form manager
            formManager = new FormManagement();
        }

        static void qlOtherSettings_SettingChanged(object sender, SettingChangedEventArgs e)
        {
            string excludes = qlOtherSettings.GetValue(Resources.ExcludeExt), ext;
            List<string> excludeExts = new List<string>();
            if (!string.IsNullOrEmpty(excludes))
            {
                excludes = excludes.Trim().ToLower();
                foreach (string extTmp in excludes.Split(';'))
                {
                    ext = extTmp.Trim();
                    if (!string.IsNullOrEmpty(ext))
                        excludeExts.Add(ext);
                }
            }
            fileNameSearch.ExcludeExts = excludeExts;
        }

        static void qlDirSetting_SettingChanged(object sender, EventArgs e)
        {
            fileNameSearch.Directories = qlDirSetting.GetSettingItems();
        }

        static void qlShortcutSetting_SettingChanged(object sender, EventArgs e)
        {
            
        }

        #endregion

        public static SearchBoxForm mainForm = null;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // initialization
            Program.Initialize();

            using (KeyboardHook keyHook = new KeyboardHook("PassAllKeysToNextApp"))
            {
                // register the key press event
                keyHook.KeyIntercepted += new KeyboardHook.KeyboardHookEventHandler(keyHook_KeyIntercepted);

                // start the main form
                mainForm = new SearchBoxForm();
                //Application.Run(mainForm);
                SingleApplication.Run(mainForm);
            }
        }

        static void keyHook_KeyIntercepted(KeyboardHook.KeyboardHookEventArgs e)
        {
            if ((e.KeyName == "Q" && e.Modifier == PressModifier.LWin))
            {
                mainForm.ActivateSearchBox();
            }
            else if (e.KeyName == "Escape" && e.Modifier== PressModifier.None &&
                mainForm.Visible == true)
            {
                mainForm.DeactivateSearchBox();
            }
        }
    }
}